package com.sj.stone.controller;


import com.sj.stone.domain.oracle.*;
import com.sj.stone.domain.oracle.entry.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/order")
public class OrderController {

    @Autowired
    private RegistDataRepository registDataRepository;
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private WaterOrderDetailsRepository waterOrderDetailsRepository;
    @Autowired
    private OrderSignForRepository orderSignForRepository;
    @Autowired
    private SalesManRepository salesManRepository;
    @Autowired
    private SalesUserRepository salesUserRepository;
    @Autowired
    private MemberRepository memberRepository;

    //订单信息查询
    @RequestMapping("/findOrder")
    @ResponseBody
    public StringBuffer findOrder(String orderN) {
        StringBuffer str = new StringBuffer();
        Order order = new Order();
        OrderSignfor orderSignfor = new OrderSignfor();
        try {
            order = orderRepository.findAllByOrderNum(orderN.trim());
            Member member = memberRepository.findOne(order.getMemberId());
            str.append("到货状态:" + order.getShipStatus()+",订单状态:"+order.getStatus()+",订单总金额:"+order.getAmount()+
                    ",钱包支付金额:"+order.getWalletNum() +",实际支付金额:"+order.getActualPayNum()+
                    ",付款状态:" + order.getPayStatus() + ",支付类型:" + order.getPayMent() + ",店铺手机号:" + member.getMobile() + "!!!!!我是分割线!!!!!!");
        } catch (Exception e) {
            str.append("--订单号不唯一--");
            e.printStackTrace();
        }
        try {
            orderSignfor = orderSignForRepository.findAllByOrderNo(orderN.trim());
            SalesMan salesMan = salesManRepository.findOne(orderSignfor.getUserId());
            str.append("快递单号:" + orderSignfor.getFastmailNo() + ",app操作情况:" + orderSignfor.getOrderStatus() +
                    ",付款方式:" + orderSignfor.getOrderPayType() +",订单金额"+orderSignfor.getOrderPrice()+",订单状态:"+orderSignfor.getRelatedStatus()+
                    ",订单实际金额:"+orderSignfor.getActualPayNum()+ ",负责业务:" + salesMan.getTruename() +
                    ",订单预留店铺手机号:" + orderSignfor.getMemberPhone() + ",");
            RegistData registData = registDataRepository.findByLoginAccount(orderSignfor.getMemberPhone());
            StringBuffer rele = new StringBuffer();
            if (registData != null) {
                rele.append("已关联");
            } else {
                rele.append("未关联");
            }
            str.append("店铺状态:" + rele);
        } catch (Exception e) {
            str.append("--订单号不唯一--");
            e.printStackTrace();
        }

        return str;
    }

    //客户未付款订单查询
    @RequestMapping("/lookOrder")
    @ResponseBody
    public String lookOrder(String phone) {
        String cbbc = "订单号:";
        String orderNum = null;
        List orderList = orderRepository.lookOrderNo(phone.trim());

        for (int i = 0; i < orderList.size(); i++) {
            if (orderNum == null) {
                orderNum = "\"" + (String) orderList.get(i);
            } else {
                orderNum = orderNum + "\",\"" + (String) orderList.get(i);
            }
        }
        return cbbc + orderNum + "\"";
    }

    @RequestMapping("/lookDl")
    @ResponseBody
    public String lookDl(String dlNo) {
        String cbbc = "订单号:";
        String orderNum = null;

        List<WaterOrderDetails> wodlist = waterOrderDetailsRepository.findBySerialno(dlNo.trim());

        List list = new ArrayList();

        for (int i = 0; i < wodlist.size(); i++) {
            OrderSignfor osf = orderSignForRepository.findOne(wodlist.get(i).getCashid());
            list.add(osf.getOrderNo());
        }
        for (int i = 0; i < list.size(); i++) {
            if (orderNum == null) {
                orderNum = "\"" + (String) list.get(i);
            } else {
                orderNum = orderNum + "\",\"" + (String) list.get(i);
            }
        }

        return cbbc + orderNum + "\"";
    }

    @RequestMapping("/findDl")
    @ResponseBody
    public String findDl(String orderNo) {
        String cbbc = "";
        OrderSignfor orderSignfor = orderSignForRepository.findAllByOrderNo(orderNo.trim());
        WaterOrderDetails waterOrderDetails = waterOrderDetailsRepository.findByCashid(orderSignfor.getId());
        if (waterOrderDetails != null) {
            cbbc = waterOrderDetails.getSerialno();
        } else {
            cbbc = "该订单不是收现金";
        }

        return cbbc;
    }

    @RequestMapping("/upRstatus")
    @ResponseBody
    public String upRstatus(String orderNo) {

        String cbbc = "状态已修改";
        OrderSignfor orderSignfor = orderSignForRepository.findAllByOrderNo(orderNo.trim());
        if (orderSignfor != null) {
            RegistData registData = registDataRepository.findByLoginAccount(orderSignfor.getMemberPhone());
            if (registData != null) {
                orderSignfor.setRelatedStatus(OrderSignfor.RelatedStatus.ENDRELATED);
            } else {
                cbbc = "该店铺尚未关联";
            }
        } else {
            cbbc = "订单号输入错误";
        }
        return cbbc;
    }

    @RequestMapping("/updal")
    @ResponseBody
    public String updal(String orderNo) {

        String cbbc = "OK";
        try {
            Order order = orderRepository.findAllByOrderNum(orderNo.trim());
            OrderSignfor orderSignfor = orderSignForRepository.findAllByOrderNo(orderNo.trim());
            order.setShipStatus(Order.ShipStatus.RECEIVED);
            order.setPayStatus(Order.PayStatus.PAID);
            orderRepository.save(order);
            orderSignfor.setOrderStatus(OrderSignfor.OrderStatus.MEMBERSIGNFO);
            orderSignfor.setOrderPayType(OrderSignfor.OrderPayType.POS);
            orderSignfor.setCustomSignforException(0);
            orderSignfor.setDealType("POS");
            //时间计算-->增加了5个小时
            Calendar cal = Calendar.getInstance();
            Date date = orderSignfor.getYewuSignforTime();
            cal.setTime(date);
            cal.add(Calendar.HOUR, 5);// 24小时制
            date = cal.getTime();
            //-------------
            orderSignfor.setCustomSignforTime(date);
            orderSignfor.setBushPoseTime(date);
            orderSignForRepository.save(orderSignfor);
        } catch (Exception e) {
            cbbc = "NO";
            e.printStackTrace();
        }

        return cbbc;
    }

    @RequestMapping("/updno")
    @ResponseBody
    public String updno(String orderNum) {
        String cbbc = "OK";
        try {
            Order order = orderRepository.findAllByOrderNum(orderNum.trim());
            OrderSignfor orderSignfor = orderSignForRepository.findAllByOrderNo(orderNum.trim());
            order.setShipStatus(Order.ShipStatus.ARRIVED);
            order.setPayStatus(Order.PayStatus.NON);
            orderRepository.save(order);
            orderSignfor.setOrderStatus(OrderSignfor.OrderStatus.YWSIGNFOR);
            orderSignfor.setOrderPayType(null);
            orderSignfor.setCustomSignforTime(null);
            orderSignfor.setCustomSignforGeopoint(null);
            orderSignfor.setDealType(null);
            orderSignfor.setBushPoseTime(null);
            orderSignForRepository.save(orderSignfor);
        } catch (Exception e) {
            cbbc = "NO";
            e.printStackTrace();
        }
        return cbbc;
    }

    @RequestMapping("/upOrderSale")
    @ResponseBody
    public String upOrderSale(String orderNo, String saleName) {
        String cbbc = "更改负责人成功";

        List<SalesUser> userlist = salesUserRepository.findByUsername(saleName.trim());


        if (userlist.size() == 1) {
            SalesMan salesMan = salesManRepository.findOne(userlist.get(0).getUserid());
            if (salesMan != null) {
                OrderSignfor orderSignfor = orderSignForRepository.findAllByOrderNo(orderNo.trim());
                orderSignfor.setUserId(salesMan.getId());
                orderSignfor.setUserPhone(salesMan.getMobile());
                orderSignForRepository.save(orderSignfor);
            } else {
                cbbc = "用户名不存在";
            }
        } else {
            cbbc = "用户名不唯一或不存在";
        }

        return cbbc;
    }


}
